<?php


namespace app\adminapi\lists\room\department;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsExcelInterface;
use app\common\lists\ListsSearchInterface;
use app\common\model\room\department\RoomDepartment;

/**
 * RoomDepartment列表
 * Class RoomDepartmentLists
 * @package app\adminapi\lists
 */
class RoomDepartmentLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExcelInterface {


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function setSearch(): array {
        return [
            '=' => ['status'],
            '%like%' => ['name', 'department_sn'],
        ];
    }

    /**
     * @notes 搜索条件
     * @
     * @date 2023/2/24 16:08
     */
    public function queryWhere() {
        $where = [];

        if (!empty($this->adminCid) && $this->adminCid !== 0) {
            $this->params['building'][0] = $this->adminCid;
        }

        if (!empty($this->params['building'])) {
            if (isset($this->params['building'][0])) {
                $where[] = ['cid', '=', $this->params['building'][0]];
            }
            if (isset($this->params['building'][1])) {
                $where[] = ['bid', '=', $this->params['building'][1]];
            }
        }
        if (!empty($this->params['department'])) {
            if (isset($this->params['department'][0])) {
                $where[] = ['cid', '=', $this->params['department'][0]];
            }
            if (isset($this->params['department'][1])) {
                $where[] = ['bid', '=', $this->params['department'][1]];
            }
        }
        return $where;
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function lists(): array {
        return RoomDepartment::where($this->searchWhere)
            ->where($this->queryWhere())
            ->field('*')
            ->with([
                "campus", "building",
            ])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['sort' => 'asc', 'id' => 'asc'])
            ->select()->toArray();
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public function count(): int {
        return RoomDepartment::where($this->searchWhere)->where($this->queryWhere())->count();
    }


    /**
     * @notes 导出文件名
     * @return string
     * @author 段誉
     * @date 2022/11/24 16:17
     */
    public function setFileName(): string {
        return '科室信息表';
    }

    public function setExcelLists() {
        return null;
    }

    /**
     * @notes 导出字段
     * @return string[]
     * @author 段誉
     * @date 2022/11/24 16:17
     */
    public function setExcelFields(): array {
        return [
            'code' => '代码',
            'name' => '科室',
            'room_s_use_total' => '用水度数',
            'room_s_price' => '水单价',
            'room_s_total_fee' => '水费',
            'room_d_use_total' => '用电度数',
            'room_d_price' => '电单价',
            'room_d_total_fee' => '电费',
            'log_date' => '时间',
        ];
    }

}
